package com.devicemantra.coordinate.ds.tests;
import static java.util.logging.Level.WARNING;

import com.devicemantra.coordinate.dataobjects.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.devicemantra.coordinate.dataobjects.CDepartmentFactory;
import com.devicemantra.coordinate.interfaces.call.CCallMessage;
import com.devicemantra.coordinate.interfaces.email.CSendEmail;
import com.devicemantra.coordinate.tasks.CTask;
import com.devicemantra.coordinate.tasks.CTaskFactory;
import com.devicemantra.coordinate.ds.tests.PMF;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@SuppressWarnings("serial")
public class TestAuthCall extends HttpServlet 
{
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException 
	{
		final  Logger log = Logger.getAnonymousLogger();
		/* First let us persist the call data to the db */
		CAuthMobileMessage lAuthCallMesg = new CAuthMobileMessage();
		lAuthCallMesg.setAuthCode("5533");
		lAuthCallMesg.setAuthEmailId("watchersparadise@gmail.com");
		lAuthCallMesg.setAuthMobileNumber("97144413952");
		lAuthCallMesg.setAuthUserName("Amith Nambiar");
		PersistenceManager lPmf = PMF.get().getPersistenceManager();
		lPmf.makePersistent(lAuthCallMesg);
		lPmf.close();  
		
		String lBody = "Your e-Coordinate authentication code is 5533. Please click on the below url to verify it.";
		String lUrl = "http://devicemantra2.appspot.com/AuthenticateMobileNumber?id=" +  lAuthCallMesg.getMessageId();
		String lEmailId = "watchersparadise@gmail.com";
		CSendEmail.sendMail(lEmailId, "e-Coordinate please verify your mobile number" , lBody + lUrl);
		     
		/*CTask lTask = CTaskFactory.createNewCallTask();
		lTask.setTaskUrl("/AuthCallTask"); 
		lTask.addParameter("mobile_number","971559266628"); 
		lTask.addParameter("call_responder_id",lAuthCallMesg.getMessageId());
		CTaskFactory.addTaskToQueue(lTask);  */
		//resp.getOutputStream().println("Add call task with id " + lAuthCallMesg.getMessageId());
		resp.getOutputStream().println("Queued Email tasks for execution !" + "<" + lEmailId + ">"); 
		log.info("Queued Email tasks for execution !" + "Message Id is ");
		log.log(WARNING, "Queued Email tasks for execution !");   
	}
}

